<?php
class OffersController extends AppController {
	var $name = 'Offers';
	var $view = 'Theme';
	var $theme = HOST;
	
	var $uses = array('ViewOffer','Offer');

	
	public $presetVars = array(
		array('field' => 'typ_id', 'type' => 'value'),
		array('field' => 'transakcja_id', 'type' => 'value'),
		array('field' => 'rynek', 'type' => 'value'),
		array('field' => 'cena_od', 'type' => 'value'),
		array('field' => 'cena_do', 'type' => 'value'),
		array('field' => 'powierzchnia_od', 'type' => 'value'),
		array('field' => 'powierzchnia_do', 'type' => 'value')
	);
	
	public function beforeFilter(){
		parent::beforeFilter();
	}
	
	function index($filtr=null) {
		switch($filtr){
			case 'sprzedaz-mieszkan':				
				$typ_id = 0;
				$transakcja_id = array(0,1);
				$rzeszow = null;
				break;
			case 'wynajem-mieszkan':
				$typ_id = 0;
				$transakcja_id = 2;
				$rzeszow = null;
				break;	
			case 'mieszkania-developerskie':
				$typ_id = 0;
				$transakcja_id = array(0,1);
				$rzeszow = null;
				break;
			case 'domy-rzeszow':
				$typ_id = 1;
				$transakcja_id =  array(0,1);
				$rzeszow = 1;
				break;
			case 'domy-poza-rzeszowem':
				$typ_id = 1;
				$transakcja_id = array(0,1);
				$rzeszow = 0;
				break;
			case 'wynajem-domow':
				$typ_id = 1;
				$transakcja_id = 2;
				$rzeszow = null;
				break;
			case 'dzialki-w-rzeszowie':
				$typ_id = 2;
				$transakcja_id = array(0,1);
				$rzeszow = 1;
				break;
			case 'dzialki-poza-rzeszowem':
				$typ_id = 2;
				$transakcja_id = array(0,1);
				$rzeszow = 0;
				break;
			case 'dzialki-komercyjne':
				$typ_id = 8;
				$transakcja_id = array(0,1);
				$rzeszow = null;
				break;
			case 'lokale-obiekty-sprzedaz':
				$typ_id = 3;
				$transakcja_id = array(0,1);
				$rzeszow = null;
				break;
			case 'lokale-obiekty-wynajem':
				$typ_id = 3;
				$transakcja_id = 2;
				$rzeszow = null;
				break;
			case 'lokale-obiekty-komercyjne':
				$typ_id = 9;
				$transakcja_id = array(0,1);
				$rzeszow = null;				
				break;
			default:
				$typ_id = null;
				$transakcja_id = null;
				$rzeszow = null;
				break;
		}
		$conditions = array();
// 		$this->loadModel('ViewOffer');
		if(!is_null($typ_id)) $conditions['ViewOffer.typ_id'] = $typ_id;
		if(!is_null($transakcja_id)) $conditions['ViewOffer.transakcja_id'] = $transakcja_id;
		if(!is_null($rzeszow) && $rzeszow == 1){
			$conditions['ViewOffer.miasto'] = 'Rzeszów';
		}elseif (!is_null($rzeszow) && $rzeszow == 0){
			$conditions['ViewOffer.miasto !='] = "Rzeszów";
		}
		
		$this->ViewOffer->recursive = 0;	
		$this->paginate = array('conditions' => $conditions,'limit' => 12,'order'=>'ViewOffer.wprowadzono DESC');
		$this->set('offers', $this->paginate('ViewOffer'));
	}
	
	function search(){
		$this->ViewOffer->set( $this->data );
		$this->Prg->commonProcess();
		$conditions =  $this->ViewOffer->parseCriteria($this->passedArgs);
		
		$this->ViewOffer->recursive = 0;
		$this->paginate = array('conditions' => $conditions,'limit' => 12,'order'=>'ViewOffer.wprowadzono DESC');
		
		
		$this->set('offers', $this->paginate('ViewOffer'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid offer', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->loadModel('OffersColumn');
		
		$offer = $this->Offer->read(null, $id);
		
		switch($offer['Typ']['id']){
			case 0:
				$column = 'mieszkania';
				break;
			case 1:
				$column = 'domy';
				break;	
			case 2:
				$column = 'dzialki';
				break;
			case 3:
				$column = 'lokale';
				break;	
			case 5:
				$column = 'lokale';
				break;
			case 8:
				$column = 'dzialki';
				break;
			case 9:
				$column = 'lokale';
				break;
			default:
				$column = 'mieszkania';
				break;
		}
		$columns_ = $this->OffersColumn->find('all',array('fields'=>array('OffersColumn.id','OffersColumn.'.$column,'OffersColumn.opis_columny'),'conditions'=>array('OffersColumn.'.$column => 1)));
		foreach ($columns_ as $row) {
			$columns[$row['OffersColumn']['id']] = array('onoff'=>$row['OffersColumn'][$column],'name'=>$row['OffersColumn']['opis_columny']);
		}
		
		$conditions = array();
		$conditions['ViewOffer.typ_id'] = $offer['Typ']['id'];
		$conditions['ViewOffer.transakcja_id'] = $offer['Transakcja']['id'];
		
		$this->ViewOffer->recursive = 0;
		$this->paginate = array('conditions' => $conditions,'limit' => 6,'order'=>'ViewOffer.cena_od');
		$this->set('offers', $this->paginate('ViewOffer'));
		
		$this->set('offer', $offer);
		if(isset($columns)) $this->set('columns', $columns);
	}

	function add() {
		if (!empty($this->data)) {
			$this->Offer->create();
			if ($this->Offer->save($this->data)) {
				$this->Session->setFlash(__('The offer has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The offer could not be saved. Please, try again.', true));
			}
		}
		$typs = $this->Offer->Typ->find('list');
		$transakcjas = $this->Offer->Transakcja->find('list');
		$images = $this->Offer->Image->find('list');
		$this->set(compact('typs', 'transakcjas', 'images'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid offer', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Offer->save($this->data)) {
				$this->Session->setFlash(__('The offer has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The offer could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Offer->read(null, $id);
		
		
			switch($this->data['Typ']['id']){
				case 0:
					$column = 'mieszkania';
					break;
				case 1:
					$column = 'domy';
					break;
				case 2:
					$column = 'dzialki';
					break;
				case 3:
					$column = 'lokale';
					break;
				case 5:
					$column = 'lokale';
					break;
				case 8:
					$column = 'dzialki';
					break;
				case 9:
					$column = 'lokale';
					break;
				default:
					$column = 'mieszkania';
				break;
			}
			$this->loadModel('OffersColumn');
			$columns_ = $this->OffersColumn->find('all',array('fields'=>array('OffersColumn.id','OffersColumn.'.$column,'OffersColumn.opis_columny'),'conditions'=>array('OffersColumn.'.$column => 1)));
			foreach ($columns_ as $row) {
				$columns[$row['OffersColumn']['id']] = array('onoff'=>$row['OffersColumn'][$column],'name'=>$row['OffersColumn']['opis_columny']);
			}
			if(isset($columns)) $this->set('columns', $columns);
		}
		
		$typs = $this->Offer->Typ->find('list');
		$transakcjas = $this->Offer->Transakcja->find('list');
		$images = $this->Offer->Image->find('list');
		$this->set(compact('typs', 'transakcjas', 'images'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for offer', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Offer->delete($id)) {
			$this->Session->setFlash(__('Offer deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Offer was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	

}
